<template>
  <div class="article-list">
    <van-pull-refresh v-model="isLoading" @refresh="refresh">
      <van-list
        v-if="bol"
        v-model="loading"
        :finished="finished"
        finished-text="没有更多数据了!"
        @load="getData"
      >
        <ArticileItem
          v-for="item in list"
          :key="item.art_id"
          :item="item"
        ></ArticileItem>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import ArticileItem from '@/views/home/components/artiicle-item.vue'
import { articles } from '@/api/home.js'
export default {
  name: 'ArticleList',
  data () {
    return {
      list: [],
      timestamp: Date.now(),
      loading: false,
      finished: false,
      isLoading: false,
      bol: true
    }
  },
  components: {
    ArticileItem
  },
  props: ['id'],
  // created () {
  //   this.getData()
  // },
  methods: {
    async getData () {
      const res = await articles({
        channel_id: this.id,
        timestamp: this.timestamp
      })
      this.list.push(...res.data.results)
      this.loading = false
      if (res.data.pre_timestamp) {
        this.timestamp = res.data.pre_timestamp
      } else {
        this.finished = true
      }
      console.log('星闻列表', res)
    },
    async refresh () {
      this.loading = false
      this.finished = false
      this.timestamp = Date.now()
      this.list = []
      await this.getData()
      this.isLoading = false
      this.bol = false
      this.$nextTick(() => {
        this.bol = true
      })
    }
  }
}
</script>

<style scoped lang="less">
.article-list {
  height: 100%;
  overflow-y: auto;
  padding: 0 16px;
}
</style>
